using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.UI;
using mojoPortal.Business.WebHelpers;
using eStore.Business;
using System.Collections.Generic;
using System.Text;

namespace eStore.UI
{

    public partial class eStoreMenuModule : SiteModuleControl
    {
        protected ModuleTitleControlCustom TitleControl;
        protected Store store;
        protected eStoreConfiguration config = new eStoreConfiguration();
        private int loadedStoreModuleId = -1;
        private int loadedStorePageId = -1;
        readonly eStoreDB repository = new eStoreDB();

        #region OnInit

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Site != null && Site.DesignMode) { return; }

            Load += Page_Load;
            LoadSettings();
       }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Site != null && Site.DesignMode) { return; }
            if (!Page.IsPostBack)
            {
                SetupCss();
                PopulateLabels();
                PopulateControls();
            }
        }

        private void PopulateControls()
        {
            if (store.Guid == Guid.Empty)
            {
                // store not created yet
                return;
            }

            if (store.IsClosed)
            {
                return;
            }

            PopulateMenu();

        }

        private void PopulateMenu()
        {
            List<ud_eStoreCategory> rootItems = repository.GeteStoreRootCategoriesByModuleID(loadedStoreModuleId);
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul id='" + ModuleId + "' class='treeview-menu'>");
            foreach (ud_eStoreCategory t in rootItems)
            {
                sb.Append("<li><span><a href='" + SiteRoot + "/eStore/ProductList.aspx?pageid=" + loadedStorePageId + "&mid=" + loadedStoreModuleId + "&catid=" + t.CategoryID + "' >" + t.Name + "</a></span>");
                PopulateSubMenu(t.CategoryID, sb);
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            ltrMenu.Text = sb.ToString();
        }

        private void PopulateSubMenu(int categoryID, StringBuilder sb)
        {
            List<ud_eStoreCategory> subItems = repository.GeteStoreChildCategoryByCatID(categoryID);
            if (subItems.Count > 0)
            {
                sb.Append("<ul>");
                foreach (ud_eStoreCategory t in subItems)
                {
                    sb.Append("<li><span><a href='" + SiteRoot + "/eStore/ProductList.aspx?pageid=" + loadedStorePageId + "&mid=" + loadedStoreModuleId + "&catid=" + t.CategoryID + "' >" + t.Name + "</a></span>");
                    PopulateSubMenu(t.CategoryID, sb);
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
        }


        private void PopulateLabels()
        {

        }

        private void SetJQueryScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script src='" + ImageSiteRoot + "/ClientScript/jqmojo/jquery.cookie.js' type='text/javascript'></script>");
            sb.Append("<script src='" + ImageSiteRoot + "/ClientScript/jqmojo/jquery.treeview.js' type='text/javascript'></script>");
            sb.Append("<script type='text/javascript'>");
            sb.Append("$(document).ready(function(){");
            sb.Append("$('#" + ModuleId + "').treeview({");
            sb.Append("animated: 'fast', ");
            sb.Append("collapsed: true, ");
            sb.Append("unique: true, ");
            sb.Append("persist: 'cookie'});});");
            sb.Append("</script>");
            
            if(!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "eStoreMenu"))
            {
                Page.ClientScript.RegisterStartupScript(typeof (Page), "eStoreMenu", sb.ToString());
            }
		}

        private void LoadSettings()
        {
            config = new eStoreConfiguration(Settings);
            SetJQueryScript();
            siteSettings = CacheHelper.GetCurrentSiteSettings();

            string loadedCategory = config.EStoreLoaderSelectorSetting;
            if (!loadedCategory.Equals(string.Empty))
            {
                int.TryParse(loadedCategory.Remove(loadedCategory.IndexOf("-")), out loadedStoreModuleId);
                int.TryParse(loadedCategory.Remove(0, loadedCategory.IndexOf("-") + 1), out loadedStorePageId);
            }
            
            store = new Store(siteSettings.SiteGuid, loadedStoreModuleId);
            pnlWrapper.CssClass = config.CustomCssClassSetting;

        }

        protected virtual void SetupCss()
        {
            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("styleestore") != null) return;
            Literal cssLink = new Literal
            {
                ID = "styleestore",
                Text = @"<link href='"
                       + SiteUtils.GetSkinBaseUrl()
                       +
                       @"styleestore.css' type='text/css' rel='stylesheet' media='screen' />"
            };
            stylesheet.Controls.Add(cssLink);
        }

    }
}